<?php
/**
 * 直接测试语音播放 - 诊断工具
 * 运行：php test_voice_direct.php
 */

error_reporting(E_ALL);
ini_set('display_errors', 'on');

echo "========================================\n";
echo "   语音播放诊断工具\n";
echo "========================================\n\n";

// 检查操作系统
echo "1. 检查操作系统...\n";
echo "   PHP_OS: " . PHP_OS . "\n";
echo "   是否Windows: " . (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? '是' : '否') . "\n\n";

// 检查语音文件
echo "2. 检查语音文件...\n";
$voiceDir = __DIR__ . '/voice';
$files = [
    'checkin_success.mp3',
    'checkin_success.wav',
    'checkin_end.mp3',
    'checkin_end.wav',
    'checkin_duplicate.mp3',
    'checkin_duplicate.wav',
];

foreach ($files as $file) {
    $path = $voiceDir . '/' . $file;
    $exists = is_file($path);
    $size = $exists ? filesize($path) : 0;
    echo "   {$file}: " . ($exists ? "✓ 存在 ({$size} bytes)" : "✗ 不存在") . "\n";
}
echo "\n";

// 测试 PowerShell
echo "3. 测试 PowerShell...\n";
$testCommand = 'powershell -Command "Write-Output \'PowerShell OK\'"';
$output = [];
$returnVar = 0;
exec($testCommand, $output, $returnVar);
echo "   返回值: {$returnVar}\n";
echo "   输出: " . implode("\n", $output) . "\n\n";

// 测试播放 MP3
echo "4. 测试播放 MP3 文件...\n";
$mp3File = $voiceDir . '/checkin_success.mp3';
if (is_file($mp3File)) {
    echo "   文件路径: {$mp3File}\n";
    $realPath = realpath($mp3File);
    echo "   真实路径: {$realPath}\n";
    
    // 方法1：使用 WPF MediaPlayer（异步）
    echo "\n   方法1：WPF MediaPlayer (异步)\n";
    $uri = 'file:///' . ltrim(str_replace('\\', '/', $realPath), '/');
    echo "   URI: {$uri}\n";
    
    $psCommand = 'Add-Type -AssemblyName presentationCore;'
        . ' $p = New-Object System.Windows.Media.MediaPlayer;'
        . ' $p.Open([Uri]"' . $uri . '");'
        . ' $p.Volume = 1.0; $p.Play(); Start-Sleep -Milliseconds 3000;';
    
    echo "   执行中...\n";
    $command = 'powershell -WindowStyle Hidden -Command "' . $psCommand . '"';
    pclose(popen("start /B " . $command, "r"));
    echo "   已发送播放命令（异步）\n";
    sleep(1);
    
    // 方法2：使用 WPF MediaPlayer（同步）
    echo "\n   方法2：WPF MediaPlayer (同步)\n";
    $psScript = <<<'PS'
Add-Type -AssemblyName presentationCore
$player = New-Object System.Windows.Media.MediaPlayer
$player.Open([Uri]"FILE_URI")
$player.Volume = 1.0
$player.Play()
Start-Sleep -Milliseconds 3000
$player.Stop()
$player.Close()
PS;
    $psScript = str_replace('FILE_URI', $uri, $psScript);
    
    echo "   执行中...\n";
    $output = [];
    exec('powershell -Command "' . addslashes($psScript) . '"', $output, $returnVar);
    echo "   返回值: {$returnVar}\n";
    if (!empty($output)) {
        echo "   输出: " . implode("\n", $output) . "\n";
    }
    
    // 方法3：使用 mciSendString (最可靠)
    echo "\n   方法3：使用 mciSendString (推荐)\n";
    $psScript = <<<'PS'
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class AudioPlayer {
    [DllImport("winmm.dll")]
    private static extern long mciSendString(string command, System.Text.StringBuilder returnValue, int returnLength, IntPtr winHandle);
    
    public static void Play(string file) {
        mciSendString("open \"" + file + "\" type mpegvideo alias MediaFile", null, 0, IntPtr.Zero);
        mciSendString("play MediaFile wait", null, 0, IntPtr.Zero);
        mciSendString("close MediaFile", null, 0, IntPtr.Zero);
    }
}
"@
[AudioPlayer]::Play("FILE_PATH")
PS;
    $psScript = str_replace('FILE_PATH', str_replace('\\', '\\\\', $realPath), $psScript);
    
    echo "   执行中...\n";
    $output = [];
    exec('powershell -Command "' . addslashes($psScript) . '"', $output, $returnVar);
    echo "   返回值: {$returnVar}\n";
    if (!empty($output)) {
        echo "   输出: " . implode("\n", $output) . "\n";
    }
    
} else {
    echo "   ✗ MP3 文件不存在\n";
}

// 测试播放 WAV
echo "\n5. 测试播放 WAV 文件...\n";
$wavFile = $voiceDir . '/checkin_success.wav';
if (is_file($wavFile)) {
    echo "   文件路径: {$wavFile}\n";
    $realPath = realpath($wavFile);
    echo "   真实路径: {$realPath}\n";
    
    // 使用 SoundPlayer
    echo "\n   使用 SoundPlayer\n";
    $psCommand = '(New-Object Media.SoundPlayer "' . str_replace('\\', '/', $realPath) . '").PlaySync()';
    
    echo "   执行中...\n";
    $output = [];
    exec('powershell -Command "' . $psCommand . '"', $output, $returnVar);
    echo "   返回值: {$returnVar}\n";
    if (!empty($output)) {
        echo "   输出: " . implode("\n", $output) . "\n";
    }
} else {
    echo "   ✗ WAV 文件不存在\n";
}

// 测试 TTS
echo "\n6. 测试 Windows TTS...\n";
$text = '测试语音';
$psCommand = 'Add-Type -AssemblyName System.Speech; ' .
             '$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer; ' .
             '$speak.Rate = 0; ' .
             '$speak.Volume = 100; ' .
             '$speak.Speak(\'' . $text . '\');';

echo "   文本: {$text}\n";
echo "   执行中...\n";
$output = [];
exec('powershell -Command "' . $psCommand . '"', $output, $returnVar);
echo "   返回值: {$returnVar}\n";
if (!empty($output)) {
    echo "   输出: " . implode("\n", $output) . "\n";
}

echo "\n========================================\n";
echo "   诊断完成\n";
echo "========================================\n";

